package tw.com.softleader.book.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tw.com.softleader.book.entity.Book;

@Repository
public class BookDao {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public List<Book> findAll() {
		Session session = sessionFactory.getCurrentSession();
		List<Book> books = session.createCriteria(Book.class).list();
		return books;
				
	}
	
	public Book findOne(Long id) {
		Session session = sessionFactory.getCurrentSession();
		Book book = (Book)session.get(Book.class, id);
		return book;
		
	}
	
	public void insert(Book book) {
		Session session = sessionFactory.getCurrentSession();
		session.persist(book);
		
	}
	
	public void update(Book book) {
		Session session = sessionFactory.getCurrentSession();
		session.merge(book);
	}
	
	public void delete(Book book) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(book);
	}
	
	

}
